此文來自這位 youtuber BK Codes 的內容;但其實也不算翻譯,因為他的印度口音我還真的完全對不上頻率!! 因此主要為以我為小白角度將 code 上註解,若有誤解之處歡迎各位讀著更正賜教。
第一篇為 Django 部分,記得先額外安裝好 rest_framework!(第二篇 FLUTTER 部分)
todo_app
|_ apis
|_ todo_app
|_ todos
ALLOWED_HOSTS = [
'10.0.2.2', # for Android emulator
'localhost'
]
INSTALLED_APPS = [
...
'apis.apps.ApisConfig',
'rest_framework',
'todos.apps.TodosConfig'
]
REST_FRAMEWORK = { # rest_framework 權限無需認證,無腦全開
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
]
}
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('apis/v1/', include('apis.urls')), # 開路給 apis/uls.py
]
from django.db import models
# 給 flutter listview 的 title / subtitle
class Todo(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
def __str__(self):
return self.title
from rest_framework import serializers
from todos import models
class TodoSerializer(serializers.ModelSerializer):
class Meta:
fields = (
'id', # 給 flutter 指定資料庫資料
'title',
'description'
)
model = models.Todo
from django.urls import path
from .views import ListTodo, DetailTodo
urlpatterns = [
path('', ListTodo.as_view()),
path('<int:pk>/', DetailTodo.as_view())
]
from django.shortcuts import render
from rest_framework import generics
from todos import models
from .serializers import TodoSerializer
class ListTodo(generics.ListCreateAPIView):
queryset = models.Todo.objects.all() # 指定數據對象查詢集
serializer_class = TodoSerializer # 指定序列化器
class DetailTodo(generics.RetrieveUpdateDestroyAPIView):
queryset = models.Todo.objects.all()
serializer_class = TodoSerializer
]
# rest-framework 把操作都包裝好好,指定好名稱便可
# 'ListCreateAPIView': get、post
# 'RetrieveUpdateDestroyAPIView': get、put、patch、delete
# https://www.django-rest-framework.org/api-guide/generic-views/
python manage.py runserver 0.0.0.0:8000
第二篇 FLUTTER 部分
註: 資料出處
Part1: https://www.youtube.com/watch?v=hfee7SIwUTs
Part2: https://www.youtube.com/watch?v=fyndW3s9t6M
Part3: https://www.youtube.com/watch?v=LW-220zNA2E
GITHUB:https://github.com/bayardkalyan/flutter-django-fullstack